package pl.edu.pw.tm.zestawfiltrow.panels;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.LayoutStyle.ComponentPlacement;

import org.apache.log4j.Logger;

import pl.edu.pw.tm.zestawfiltrow.algorytmy.Average;
import pl.edu.pw.tm.zestawfiltrow.algorytmy.BW;
import pl.edu.pw.tm.zestawfiltrow.algorytmy.Edge;
import pl.edu.pw.tm.zestawfiltrow.algorytmy.Filter;
import pl.edu.pw.tm.zestawfiltrow.algorytmy.Gauss;
import pl.edu.pw.tm.zestawfiltrow.algorytmy.Median;
import pl.edu.pw.tm.zestawfiltrow.algorytmy.Negative;
import pl.edu.pw.tm.zestawfiltrow.utils.zestawFiltrow;

public class FiltersPanel extends JPanel {
	private static final long serialVersionUID = 2851213505595314069L;

	private static Logger log = Logger.getLogger(FiltersPanel.class);

	private JRadioButton radioFiltr0;
	private JRadioButton radioFiltr1;
	private JRadioButton radioFiltr2;
	private JRadioButton radioFiltr3;
	private JRadioButton radioFiltr4;
	private JRadioButton radioFiltr5;
	private JButton btnChange;

	private ImagePanel imagePanel;

	private ArrayList<JRadioButton> rbList;
	private ArrayList<Filter> filtersList = new ArrayList<>();

	private JFrame frame;

	public FiltersPanel(JFrame f, ImagePanel imgPanel) {
		setSize(zestawFiltrow.filterPanelWidth, zestawFiltrow.filterPanelHeight);
		this.imagePanel = imgPanel;
		this.frame = f;
		addFilters();

		ButtonGroup group = new ButtonGroup();
		rbList = new ArrayList<>();

		radioFiltr0 = new JRadioButton(filtersList.get(0).getName(), true);
		group.add(radioFiltr0);
		rbList.add(radioFiltr0);

		radioFiltr1 = new JRadioButton(filtersList.get(1).getName(), false);
		group.add(radioFiltr1);
		rbList.add(radioFiltr1);

		radioFiltr2 = new JRadioButton(filtersList.get(2).getName(), false);
		group.add(radioFiltr2);
		rbList.add(radioFiltr2);

		radioFiltr3 = new JRadioButton(filtersList.get(3).getName(), false);
		group.add(radioFiltr3);
		rbList.add(radioFiltr3);

		radioFiltr4 = new JRadioButton(filtersList.get(4).getName(), false);
		group.add(radioFiltr4);
		rbList.add(radioFiltr4);

		radioFiltr5 = new JRadioButton(filtersList.get(5).getName(), false);
		group.add(radioFiltr5);
		rbList.add(radioFiltr5);

		btnChange = new JButton("Ustaw");
		btnChange.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int numer = -1;
				for (int i = 0; i < rbList.size(); i++) {
					JRadioButton r = rbList.get(i);
					if (r.isSelected()) {
						numer = i;
						break;
					}
				}
				if (imagePanel.getClearImage() == null) {
					JOptionPane.showMessageDialog(frame, "Proszę wybrać zdjęcie");
				} else {
					BufferedImage image = filtersList.get(numer).run(imagePanel.getClearImage());
					imagePanel.setFilteredImage(image);
				}
			}
		});
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(Alignment.TRAILING).addGroup(
				groupLayout.createSequentialGroup().addGroup(
						groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout.createSequentialGroup().addContainerGap().addComponent(radioFiltr0)).addGroup(
								groupLayout.createSequentialGroup().addGap(56).addComponent(btnChange)).addGroup(groupLayout.createSequentialGroup().addContainerGap().addComponent(radioFiltr1))
								.addGroup(groupLayout.createSequentialGroup().addContainerGap().addComponent(radioFiltr2)).addGroup(
										groupLayout.createSequentialGroup().addContainerGap().addComponent(radioFiltr3)).addGroup(
										groupLayout.createSequentialGroup().addContainerGap().addComponent(radioFiltr4)).addGroup(
										groupLayout.createSequentialGroup().addContainerGap().addComponent(radioFiltr5))).addContainerGap(56, Short.MAX_VALUE)));
		groupLayout.setVerticalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(
				groupLayout.createSequentialGroup().addGap(9).addComponent(radioFiltr0).addPreferredGap(ComponentPlacement.RELATED).addComponent(radioFiltr1).addPreferredGap(
						ComponentPlacement.RELATED).addComponent(radioFiltr2).addPreferredGap(ComponentPlacement.RELATED).addComponent(radioFiltr3).addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(radioFiltr4).addPreferredGap(ComponentPlacement.RELATED).addComponent(radioFiltr5).addPreferredGap(ComponentPlacement.RELATED, 119, Short.MAX_VALUE)
						.addComponent(btnChange).addContainerGap()));
		setLayout(groupLayout);
	}

	private void addFilters() {
		filtersList.add(new Negative());
		filtersList.add(new Edge());
		filtersList.add(new BW());
		filtersList.add(new Average());
		filtersList.add(new Gauss());
		filtersList.add(new Median());

	}
}
